package com.sky.service.impl;

import com.google.common.collect.Lists;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        // 判断数据库中是否存在该条购物车数据
        // 通过userId和setmealId或dishId进行查询
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.select(shoppingCart);

        if (shoppingCarts != null && !shoppingCarts.isEmpty()) {
            // 存在过该购物车数据，直接将数量增加一然后更新
            ShoppingCart item = shoppingCarts.get(0);
            item.setNumber(item.getNumber() + 1);
            shoppingCartMapper.update(item);
        } else {
            // 不存在该购物车数据，组装购物车对象保存
            // 使用拷贝过属性的变量shoppingCart
            // 判断该购物车数据是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                // 属于菜品，补充购物车信息
                // 需要先查出菜品的name等信息
                Dish dish = dishMapper.selectByIds(Lists.newArrayList(dishId)).get(0);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());

            } else {
                // 属于套餐
                Setmeal setmeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            // userId使用线程获取
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        return shoppingCartMapper.select(shoppingCart);
    }

    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        // 判断数据库中是否存在该条购物车数据
        // 通过userId和setmealId或dishId进行查询
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.select(shoppingCart);
        if (shoppingCarts != null && !shoppingCarts.isEmpty()) {
            // 存在该数据，将数量减一并更新
            ShoppingCart item = shoppingCarts.get(0);
            if (item.getNumber() > 1) {
                item.setNumber(item.getNumber() - 1);
                shoppingCartMapper.update(item);
            } else {
                // 数量已经为1了，直接删除购物车数据
                shoppingCartMapper.deleteById(item.getId());
            }
        }
    }
}
